<?php
/**
 * 
 * @author tangbl
 *
 */
class ActvipcardAction extends CommonAction {
	
	public function _initialize() {
		parent::_initialize();
		parent::_initmenu();	
	}
	
	/**
	 * 列表
	 */
	public function index(){
		
		if(IS_AJAX){
			import('ORG.Util.Page');
			$model     = D("Actvipcard");
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "isdel=2 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$list      = $model->where($map)->field("privileged,instruction,total_rule,custom_rule,money_rule",true)->order($orderName.' '.$orderDesc)
						->page($nowPage.','.$pageNum)->select();
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$del = L('cfg_list_itf_btn_delete');
			$upd = L('cfg_list_itf_btn_modify');
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'          => $row['id'],
						'lang'        => $row['lang'],
						'title'       => $row['title'],
						'prefix'      => $row['prefix'],
						'totalnum'    => $row['totalnum'],
						'discount'    => $row['discount'],
						'init_jifen'  => $row['init_jifen'],
						'init_money'  => $row['init_money'],
						'addtime'     => date('Y-m-d',$row['addtime']),
						'deal'=>'
				     <a href="'.__URL__.'/edit/id/'.$row['id'].'" class="btn blue icn-only" title="'.$upd.'"><i class="icon-edit"></i></a>  
					 <a href="#" class="btn red icn-only delAlone" title="'.$del.'" data-id="'.$row['id'].'"><i class="icon-remove"></i></a>
					 <a href="'.__URL__.'/vipcardlog/vipid/'.$row['id'].'" class="btn blue icn-only" title="vipcard list"><i class="icon-credit-card"></i></a> 		
					'
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			$this->display();	
		}
	}
	
	/**
	 * 添加
	 */
	public function add(){
		if(IS_POST){
			$param = array(
				"upload" => array("single"=>array("card")),
				"reply"  => true,
			);
			parent::do_add("Actvipcard",$param);
		}else{
			
			$lang    = $this->_param("lang","",getLang());
			$this->assign("lang",$lang);
			$this->display();
		}
	}
	
	/**
	 * 删除
	 */
	public function del(){
		$param = array(
			"reply"  => true
		);
		parent::do_del("Actvipcard",false,$param);
	}
	
	/**
	 * 修改
	 */
	public function edit(){
		
		if(IS_POST){
			
			$isbase = $this->_post("isbase","intval",2);
			if($isbase==1){
				$model = M("Actvipcard");
				$array['isbase'] = 2;
				$model->where("isbase=1")->save($array);
			}
			
			$param = array(
				"upload" => array("single"=>array("card")),
				"reply"  => true,
				"arr"    => array(
						"csm_credits"   => $this->_post("init_credits"),//初始积分
						"total_credits" => $this->_post("init_credits"),//总积分
				)
			);
			parent::do_edit("Actvipcard",$param);
		}else{
			
			$model = D("Actvipcard");
			$id    = $this->_param('id','intval',0);
			if(!$id) $this->error(L('cfg_form_tipmsg_illgel'));
			
			$data   = $model->find($id); 
			$module = "Actvipcard";
			
			$card = find_thumb($data['solevar'],'card',$module);
			$this->assign("card",$card);
			
			$this->assign("Actvipcard",$data); 
			$this->display();		
		}	
	}
	
	/**
	 * 会员卡领取记录
	 */
	public function vipcardlog() {
		
		if(IS_AJAX){
			
			import('ORG.Util.Page');
			$model     = D("ActvipcardUserInfoView");
			
			$orderDesc = $this->_get("orderDirection","trim","desc");;
			$orderName = $this->_get("orderName","trim","addtime");
			$pageNum   = $this->_get("pageNum","intval",C('web_admin_pagenum'));
			$vipid     = $this->_get("vipid","intval",0);
			$nowPage   = $this->_get("p","intval",1);
			
			$map       = unescape($this->_get("where"));
			$map       = "1=1 ".$map;
			$count     = $model->where($map)->count();
			$Page      = new Page($count);
			$show      = $Page->show();
			$list      = $model->where($map)->order($orderName.' '.$orderDesc)
						 ->page($nowPage.','.$pageNum)->select();
			
			$totalPage = ceil($count/$pageNum);
			C('web_admin_pagenum',$pageNum);
			
			$jsonData  = array('page'=>$nowPage,'total'=>$count,'rows'=>array(),'totalPage'=>$totalPage);
			foreach($list AS $row){
				if($row['status']==2){
					$operate = '<a href="#" class="btn red   icn-only delAlone" title="冻结此卡" data-id="'.$row['id'].'" data-type="1"><i class="icon-lock"></i></a>';
				}else{
					$operate = '<a href="#" class="btn green icn-only delAlone" title="取消冻结" data-id="'.$row['id'].'" data-type="2"><i class="icon-unlock"></i></a>';	
				}
				
				$status = $row['status']==1?L('vcd_form_ele_status_0'):L('vcd_form_ele_status_1');
				$entry = array('id'=>$row['id'],
					'cell'=>array(
						'id'       => $row['id'],
						'sn'       => $row['vipno'],
						'username' => $row['username'],
						'realname' => $row['realname'],
						'mobile'   => $row['mobile'],
						'birthday' => $row['birthday'],
						'addtime'  => date('Y-m-d',$row['addtime']),
						'usenum'   => $row['usedtimes'],
						'status'   => $status,
						'deal'     => $operate
					),
				);
				$jsonData['rows'][] = $entry;
			}
			$this->ajaxReturn($jsonData,'JSON');
		}else{
			
			$vipid = $this->_get("vipid","intval",0);
			$this->assign("vipid",$vipid);
			
			$data  = M("Actvipcard")->find($vipid);
			$this->assign("detail",$data);
			
			$this->display();
		}
	}
	
	/**
	 * 冻结会员卡
	 */
	public function dellog(){
		
		$id     = $this->_param("id");
		$status = $this->_param("type","intval",2);
		
		$map['id']     = $id;
		$map['status'] = $status;
		$data = M("ActvipcardUser")->save($map);
		if($data){
			$this->success(L('cfg_form_tipmsg_fresucc'));
		}else{
			$this->error(L('cfg_form_tipmsg_frefail'));
		}
	}	
}
?>